package xy.server.controller;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;

import xy.server.entity.User;
import xy.server.repository.UserRespository;

@RestController
public class UseController {

	@Autowired
	private UserRespository userRespository;
	
	@Autowired
	private EurekaClient discoveryClient;
	
	@GetMapping("/simple/{id}")
	public User findById(@PathVariable Long id) {
		
		return userRespository.findOne(id);
		
	}
	
	/**
	 * 获取eureka地址
	 * @return
	 */
	@GetMapping("/getEureka-info")
	public String serviceUrl() {
		//这里填的参数是在客户端application.yml配置的别名(spring.application.name)，意思就是把自己注册到eureka上去
	    InstanceInfo instance = discoveryClient.getNextServerFromEureka("XY-SERVER", false);
	    return instance.getHomePageUrl();
	}
	
	@PostMapping("/user")
	public User postUser(@RequestBody User user) {
		
		user.setName("post user");
		user.setUsername("oooooo");
		user.setBalance(new BigDecimal(252525));
		return user;
	}
	
}
